/**
 * 
 */
package com.iwords.api.web.action;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import com.iwords.constants.Constants;
import com.iwords.service.WordServiceLocal;
import com.iwords.service.object.SysLibWordsEntity;
import com.iwords.utils.CodeUtils;
import com.iwords.utils.StringUtils;
import com.iwords.api.web.base.BaseAction;

/**
 * @author      BOLIN.HBC
 * @time        2011-3-19
 * @description 单词的SERVLET
 */
@WebServlet(name = "wordAction", urlPatterns = { "/word" })
public class WordAction extends BaseAction<SysLibWordsEntity> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@EJB
	WordServiceLocal wordService;

	public void doProcess(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException, JSONException {
		String type = req.getParameter("action");
		if (Constants.WORDS_LIST_ACTION.equals(type)) {
			doFindWordsBySysLibId(req, res);
		} else if (Constants.WORDS_SINGLE_ACTION.equals(type)) {
			doFindWordByWordId(req, res);
		} 

	}

	


	public void doFindWordsBySysLibId(HttpServletRequest req, HttpServletResponse res) throws JSONException, IOException {

		SysLibWordsEntity entity = initEntity(req);

		List<SysLibWordsEntity> resultList = wordService.findWordsBySysLibId(entity.getLibid());

		JSONObject result = new JSONObject();
		result.put(Constants.IS_SUCCESS, Boolean.TRUE);
		com.iwords.utils.JSONObject defaultValue = new com.iwords.utils.JSONObject();

		defaultValue.putColumn("wordid", "syslibid", "meaning", "phonetics", "spelling", "createtime", "updatetime");
		for (SysLibWordsEntity word : resultList) {
			defaultValue.putValue(word.getWordid(), word.getLibid(), CodeUtils.encode(word.getMeaning()),
					word.getPhonetics() == null ? "" : CodeUtils.encode(word.getPhonetics()), CodeUtils.encode(word.getSpelling()), word.getCreatetime().getTime(),
					word.getUpdatetime().getTime());

		}
		result.put(Constants.DEFAULT_VALUE, defaultValue);

		jsonPrint(res, result);

	}

	public void doFindWordByWordId(HttpServletRequest req, HttpServletResponse res) throws JSONException, IOException {

		SysLibWordsEntity entity = initEntity(req);

		SysLibWordsEntity resultEntity = wordService.findWordByWordId(entity.getWordid());
		JSONObject result = new JSONObject();
		result.put(Constants.IS_SUCCESS, Boolean.TRUE);
		if (resultEntity != null) {

			JSONObject defaultValue = new JSONObject();

			defaultValue.put("wordid", resultEntity.getWordid());
			defaultValue.put("syslibid", resultEntity.getLibid());
			defaultValue.put("meaning", CodeUtils.encode(resultEntity.getMeaning()));
			defaultValue.put("phonetics", resultEntity.getPhonetics() == null ? "" : CodeUtils.encode(resultEntity.getPhonetics()));
			defaultValue.put("spelling", CodeUtils.encode(resultEntity.getSpelling()));
			defaultValue.put("createtime", resultEntity.getCreatetime().getTime());
			defaultValue.put("updatetime", resultEntity.getUpdatetime().getTime());
			result.put("defaultValue", defaultValue);
		} else {
			result.put(Constants.IS_SUCCESS, Boolean.FALSE);
			result.put(Constants.MESSAGE, CodeUtils.encode("该单词不存在"));

		}

		jsonPrint(res, result);

	}

	public SysLibWordsEntity initEntity(HttpServletRequest req) {

		SysLibWordsEntity entity = new SysLibWordsEntity();
		String wordid = req.getParameter("wordid");
		if (StringUtils.isNotBlank(wordid)) {
			entity.setWordid(Long.parseLong(req.getParameter("wordid")));
		}
		String libid = req.getParameter("libid");
		if (StringUtils.isNotBlank(libid)) {
			entity.setLibid(Long.parseLong(req.getParameter("libid")));
		}

		return entity;
	}


}
